//
//  MoneyRecordViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/22.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "MoneyRecordViewController.h"
#import "MoneyRecordCell.h"
#import "MainTabBarController.h"
#import "RecordTableViewCell.h"
#import "MoneyRecordModel.h"

@interface MoneyRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isLastPage;
    
}

@property (nonatomic, strong) NSMutableArray *arrM;

@end
#define TITLEARR @[@"时间",@"金额",@"类型",@"状态"]
#define RecordArr @[@"时间",@"金额",@"状态"]
NSInteger currentPage = 1;
@implementation MoneyRecordViewController

- (NSMutableArray *)arrM {
    if (!_arrM) {
        _arrM = [NSMutableArray array];
    }
    return _arrM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myTableView];
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
    
    [MainTabBarController setExtraCellLineHidden:_myTableView];
    
 
  
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];

    NSLog(@"%ld", self.index);
    NSString *method = @"";
    if (self.index == 0) {
        method = @"Money.GetRechargeList";//充值记录
    } else if (self.index == 1) {
        method = @"Money.GetWithdrawalList";//提现记录
    } else if (self.index == 2) {
        method = @"Money.GetTenberList";//投标记录
    } else {
        method = @"Money.GetPayList";//放款记录  调试不成功
    }
//    [self getinvestList:method WithPage:1];
    
    
    [self refreshPageWithMethod:method];
    [self.myTableView.mj_header beginRefreshing];
   
}


- (void)getinvestList:(NSString *)method WithPage:(NSInteger)page{
    
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSString *index = [NSString stringWithFormat:@"%ld",page];
    NSDictionary *dict = @{
                           @"PageIndex":index,
                           @"LoginId":userId
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:method withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
        if (page == 1) {
            [self.arrM removeAllObjects];
            currentPage = 1;
        }
        //判断是否为最后一页
        if ([[object[@"Data"] firstObject][@"PageTotal"] integerValue]/10 <= currentPage) {
            _isLastPage = YES;
        }
        
        if (self.index == 1 || self.index == 0 ) {
            NSArray *arr = [object[@"Data"] firstObject][@"List"];
            
            for (NSDictionary *dic in arr) {
                MoneyRecordModel *model = [MoneyRecordModel mj_objectWithKeyValues:dic];
                
                [self.arrM addObject:model];
            }
            
        }else if(self.index == 2 || self.index == 3){
            NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
            
            for (NSDictionary *dic in arr) {
                MoneyRecordModel *model = [MoneyRecordModel mj_objectWithKeyValues:dic];
                
                [self.arrM addObject:model];
            }
            
        }
        
        [self.myTableView reloadData];
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:^(float progress) {
    }];
}

-(UITableView *)myTableView{
    
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-40) style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
        _myTableView.tableFooterView = [UIView new];
        _myTableView.showsVerticalScrollIndicator = NO;
       
    }
    
     return _myTableView;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrM.count;
}

//cell复用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.index == 2 ) {
        MoneyRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"];
        if (cell == nil) {
            cell = [[MoneyRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"];
        }
        
        if (self.arrM.count) {
            MoneyRecordModel *model = self.arrM[indexPath.row];
            [cell setDataWithModel:model];
        }
        
        cell.selectionStyle = UITableViewCellStyleDefault;
        return cell;
    }else if (self.index == 0 || self.index == 1 || self.index == 3){
        RecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"];
        if (cell == nil) {
            cell = [[RecordTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"];
        }
        if (self.index == 1) {
            cell.state.text = @"提现成功";     
        }
        
        if (self.arrM.count) {
            MoneyRecordModel *model = self.arrM[indexPath.row];
            [cell setDataWithModel:model];
        }
        cell.selectionStyle = UITableViewCellStyleDefault;
        return cell;
    }
    
    return nil;
   
}


//头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
    headView.backgroundColor = [UIColor whiteColor];
    UIView *lineH = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 1)];
    lineH.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.00];
    [headView addSubview:lineH];
    
    UIView *lineh = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, Screen_Width, 1)];
    lineh.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.00];
    [headView addSubview:lineh];

    
    if (self.index == 2 ) {
        for (int i = 0; i < 4; i++) {
            if (i == 0) {
                UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, Screen_Width/5 * 2, 40)];
                lable.text = TITLEARR[0];
                [headView addSubview:lable];
            }else {
                UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(Screen_Width/5 * i+ Screen_Width/5 + 20, 0, Screen_Width/5, 40)];
                lable.text = TITLEARR[i];
                [headView addSubview:lable];
            }
        }
        return headView;

    }else if (self.index == 0 || self.index == 1  || self.index == 3){
       
        UILabel *timer = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen_Width/3+20, 40)];
        UILabel *money = [[UILabel alloc] initWithFrame:CGRectMake(10+Screen_Width/3 , 0,Screen_Width/3-10, 40)];
        UILabel *state = [[UILabel alloc] initWithFrame:CGRectMake(20+Screen_Width/3 *2, 0, Screen_Width/3-10, 40)];
        
        [headView addSubview:timer];
        [headView addSubview:money];
        [headView addSubview:state];
        
        timer.text = @"时间";
        money.text = @"金额";
        state.text = @"状态";
        
        timer.font = [UIFont systemFontOfSize:17.0f];
        money.font = [UIFont systemFontOfSize:17.0f];
        state.font = [UIFont systemFontOfSize:17.0f];
        
        timer.textAlignment = NSTextAlignmentCenter;
        money.textAlignment = NSTextAlignmentRight;
        state.textAlignment = NSTextAlignmentCenter;
    }
    
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

//MJ刷新
-(void)refreshPageWithMethod:(NSString *)method
{
    
    //下拉刷新
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                     {
                                         
                                         [self getinvestList:method WithPage:1];
                                         
                                         [self.myTableView.mj_header endRefreshing];
                                     }];
    //上拉加载
    self.myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
        
//        if (_isLastPage) {
//            [self.myTableView.mj_footer endRefreshing];
//            return;
//        }
        
      
        [self getinvestList:method WithPage:++currentPage];
        
        [self.myTableView.mj_footer endRefreshing];
    }];
    
    
}

@end
